package jcrweb.as3
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.LoaderContext;
	
	import mx.controls.Alert;	
	
	public class RequestUtil{
		[Bindable]
		[Embed(source='../WebContent/images/error.gif')]
		private static var iconError:Class;
		
		private static var SERVER_PATH:String = "";
		
		public static var LOADTREE_URL:String = SERVER_PATH+"flex/loadTree";
		public static var LOADDATA_URL:String = SERVER_PATH+"flex/maindata?type=loaddata";
		
		public static var LOADLINK_URL:String = SERVER_PATH+"flex/homedata?type=loadLink";
		public static var DELETELINK_URL:String = SERVER_PATH+"flex/link?type=dellin";
		public static var CREATELINK_URL:String = SERVER_PATH+"flex/link?type=createlin";
		
		public static var LOGINCHECK_URL:String = SERVER_PATH+"flex/user?type=isLogin";
		public static var LOGIN_URL:String = SERVER_PATH+"flex/user?type=login";
		public static var LOGOUT_URL:String = SERVER_PATH+"flex/user?type=logout";
		
		public static var INDEX_PAGE_URL:String = SERVER_PATH+"index.html";
		public static var LOGIN_PAGE_URL:String = SERVER_PATH+"login.html";
		
		public static var DELETEDIR_URL:String = SERVER_PATH+"flex/directory?type=deldir";
		public static var UPDATEDIR_URL:String = SERVER_PATH+"flex/directory?type=updatedir";

		public static var INITCONTENT_URL:String = SERVER_PATH+"flex/upload";
		public static var CREATECONTENT_URL:String = SERVER_PATH+"flex/content?type=createcon";
		
		public static var CREATEDIR_URL:String = SERVER_PATH+"flex/directory?type=createdir";
		
		public static var CREATEUSER_URL:String = SERVER_PATH+"flex/user?type=createuser";

		public static var UPDATACON_URL:String = SERVER_PATH+"flex/content?type=updatecon";
		public static var DOWNCON_URL:String = SERVER_PATH+"flex/download";
		public static var DELCON_URL:String = SERVER_PATH+"flex/content?type=delcon";
		public static var CONVERSION_URL:String = SERVER_PATH+"flex/content?type=versionlst";
		
		public static var USERLIST_URL:String = SERVER_PATH+"flex/user?type=userlist";
		public static var UPDATEUSER_URL:String = SERVER_PATH+"flex/user?type=updateuser";
		public static var DELETEUSER_URL:String = SERVER_PATH+"flex/user?type=deluser";
		
		public static var SEARCH_URL:String = SERVER_PATH+"flex/search";
		
		public static var LOCALE_URL:String = SERVER_PATH+"locale";
		
		//need to update
		public static function sendRequest(url:String,strEvent:String,func:Function=null,randomUrl:Boolean=true):void{
			try{
				if(SERVER_PATH == null){
					SERVER_PATH = getServerPath();
				}
				var strUrl:String = url;
				if(randomUrl){
					strUrl = FlexUtil.randomURL(url)
				}
				var header1:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
				var header2:URLRequestHeader = new URLRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
				var loader:URLLoader = new URLLoader(); 
				var request:URLRequest = new URLRequest(strUrl); 
				var variables:URLVariables  = new URLVariables();
				
				request.method = URLRequestMethod.GET;
				//request.data = variables;
				request.requestHeaders.push(header1);
				request.requestHeaders.push(header2);

				loader.load(request);
				if(func == null){
					loader.addEventListener(strEvent, completeListener);
				}else{
					loader.addEventListener(strEvent, func);
				}
			}catch(e:Error){
				Alert.show(e.message,"Error",0,null,null,iconError);
			}
		}
		
		public static function completeListener(event:Event):void {
			var loader:URLLoader = URLLoader(event.target); 
			var isError:String = FlexUtil.getTag(loader.data,FlexUtil.IS_ERROR);
			if(isError == "true" ){
				var errorMes:String = FlexUtil.getTag(loader.data,FlexUtil.ERROR_MES_TAG);
				Alert.show("Faild!"+errorMes,"Error",0,null,null,iconError);
			}
		}
		
		public static function getServerPath():String{
//			var strUrl:String = ExternalInterface.call("window.location.href");
//			var renUrl:String = "";
//			if(strUrl.substring(0,7) != "http://"){
//				strUrl = "http://"+strUrl;
//			}
//			var num:int = 0;
//			for(var i:int;i<strUrl.length;i++){
//				if(strUrl.charAt(i)=="/"){
//					num = num + 1;
//				}
//				if(num < 3){
//					renUrl+=strUrl.charAt(i);
//				}
//			}
//			return renUrl;
			return "";
		}
	}
}